package com.example.backend.controller;


import com.example.backend.pojo.Restaurant;
import com.example.backend.pojo.Result;

import com.example.backend.pojo.User;
import com.example.backend.service.RestaurantService;
import com.example.backend.service.UserService;
import com.example.backend.util.JwtUtil;
import com.example.backend.util.Md5Util;

import com.example.backend.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/restaurant")
public class RestaurantController {
    @Autowired
    private RestaurantService restaurantService;
    @Autowired
    private UserService userService;
    
    @PostMapping("/register")//注册
    public Result register(@RequestBody Restaurant restaurant){
        Restaurant r = restaurantService.getRestaurantByName(restaurant.getRestaurantname());
        if(r==null){
            restaurantService.register(restaurant);
            return Result.success();
        }else{
            return Result.error("用户注册失败");
        }
    }

    @PostMapping("/login")//登录
    public Result<String> login(String restaurantname,String password){
        Restaurant restaurant=restaurantService.getRestaurantByName(restaurantname);
        if(restaurant==null){
            return Result.error("餐厅名错误");
        }
        if(Md5Util.getMD5String(password).equals(restaurant.getPassword())){
            Map<String ,Object> claims=new HashMap<>();
            claims.put("id",restaurant.getRestaurantid());
            claims.put("restaurantname",restaurant.getRestaurantname());
            String token = JwtUtil.genToken(claims);
            return Result.success(token);
        }
        return Result.error("密码错误");
    }

    @PostMapping("/restaurantInfo")//获得餐厅的基本信息
    public Result<Restaurant> restaurantInfo(String restaurantname) {

        return Result.success(restaurantService.getRestaurantByName(restaurantname));

    }

    @PutMapping("/update")//用户基本信息进行更新
    public Result update(@RequestBody Restaurant restaurant){
        Map<String,Object> map = ThreadLocalUtil.get();
        String restaurantid = (String) map.get("id");
        restaurant.setRestaurantid(restaurantid);
        restaurantService.update(restaurant);
        return Result.success("成功");
    }

    @PatchMapping("/updatePwd")//密码更新
    public  Result updatePwd(@RequestBody Map<String,Object> params){
        String old_pwd = (String) params.get("old_pwd");
        String new_pwd = (String) params.get("new_pwd");
        String re_pwd = (String) params.get("re_pwd");
        if(!StringUtils.hasLength(old_pwd) ||!StringUtils.hasLength(new_pwd)||!StringUtils.hasLength(re_pwd)){
            return Result.error("缺少参数");
        }
        Map<String,Map> loginrestaurant = ThreadLocalUtil.get();
        Restaurant restaurant = restaurantService.getRestaurantByName(String.valueOf(loginrestaurant.get("restaurantname")));

        if(!restaurant.getPassword().equals(Md5Util.getMD5String(old_pwd))){
            return Result.error("密码错误");
        }

        if(!new_pwd.equals(re_pwd)){
            return Result.error("新密码不相等");
        }
        new_pwd=Md5Util.getMD5String(new_pwd);
        restaurantService.updatePwd(Integer.valueOf(restaurant.getRestaurantid()),new_pwd);
        return Result.success("成功");
    }

    @PostMapping("/surplus")//商家剩余修改
    public Result surplus(Integer surplus){
        Map<String,Object> map = ThreadLocalUtil.get();
        String id = (String)map.get("id");
        restaurantService.surplus(id,surplus);
        return Result.success();
    }

    @PostMapping("/search")//商家搜索
    public Result<List<Restaurant>> search(String restaurantname){
        System.out.println(restaurantname);
        return Result.success(restaurantService.search(restaurantname));

    }

    @PostMapping("/normal")
    public Result normal(Integer restaurantid){
        System.out.println(restaurantid);
        restaurantService.normal(restaurantid);
        return Result.success();
    }

    @PostMapping("/relax")
    public Result relax(Integer restaurantid){
        restaurantService.relax(restaurantid);
        return Result.success();
    }

    @PostMapping("/getclient")
    public Result<List<User>> getclient(String restaurantid){
        return Result.success(restaurantService.getclient(restaurantid));
    }

    @PostMapping("/reccommendation")
    public Result<List<Restaurant>> reccommendation(String username){
        //曾经购物过的、用户类型表中的
        User u = userService.getUserByName(username);

        return Result.success(restaurantService.reccommendation(u.getUserid(),u.getUsertypeid()));
    }

    @PostMapping("/getmoney")
    public  Result<Double> getmoney(String restaurantid){
        return Result.success(restaurantService.getmoney(restaurantid));
    }
    @PostMapping("/updatemoney")
    public  Result<Double> updatemoney(Double money,String restaurantid){
        restaurantService.updatemoney(money,restaurantid);
        return Result.success();
    }
}
